package com.landleaf.smarthome.db.manager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.landleaf.smarthome.App;
import com.landleaf.smarthome.model.DaoMaster;
import com.landleaf.smarthome.model.DaoSession;

import org.greenrobot.greendao.query.QueryBuilder;


/**
 * When I wrote this, only God and I understood what I was doing
 * Now, God only knows
 * Created by 海航
 * 2019/9/2
 * 功能描述:
 */
public class DbManager {
    // 是否加密
    private Context context;
    private static final String DB_NAME = "landleaf.db";
    private static volatile DbManager mDbManager;
    private static DaoMaster.DevOpenHelper mDevOpenHelper;
    private static volatile DaoMaster mDaoMaster;
    private static volatile DaoSession mDaoSession;

    private DbManager() {
        this.context = App.getInstance();
        // 初始化数据库信息
        mDevOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME);
        getDaoMaster();
        getDaoSession();
    }

    public static DbManager getInstance() {
        if (null == mDbManager) {
            synchronized (DbManager.class) {
                if (null == mDbManager) {
                    mDbManager = new DbManager();
                }
            }
        }
        return mDbManager;
    }

    /**
     * 打开输出日志，默认关闭
     */

    public static void setDebug() {

        QueryBuilder.LOG_SQL = true;

        QueryBuilder.LOG_VALUES = true;

    }

    /**
     * 获取可读数据库
     *
     * @param
     * @return
     */
    public static SQLiteDatabase getReadableDatabase() {
        if (null == mDevOpenHelper) {
            getInstance();
        }
        return mDevOpenHelper.getReadableDatabase();
    }

    /**
     * 获取可写数据库
     *
     * @param
     * @return
     */
    public static SQLiteDatabase getWritableDatabase() {
        if (null == mDevOpenHelper) {
            getInstance();
        }

        return mDevOpenHelper.getWritableDatabase();
    }

    /**
     * 获取DaoMaster
     *
     * @param
     * @return
     */
    public static DaoMaster getDaoMaster() {
        if (null == mDaoMaster) {
            synchronized (DbManager.class) {
                if (null == mDaoMaster) {
                    //更新数据库
//                   MyOpenHelper helper = new MyOpenHelper(context,DB_NAME,null);
//                    mDaoMaster = new DaoMaster(helper.getWritableDatabase());
                    mDaoMaster = new DaoMaster(getWritableDatabase());
                }
            }
        }
        return mDaoMaster;
    }

    /**
     * 获取DaoSession
     *
     * @param
     * @return
     */
    public static DaoSession getDaoSession() {
        if (null == mDaoSession) {
            synchronized (DbManager.class) {
                mDaoSession = getDaoMaster().newSession();
            }
        }

        return mDaoSession;
    }

    /**
     * 关闭所有的操作，数据库开启后，使用完毕要关闭
     */

    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    public void closeHelper() {
        if (mDevOpenHelper != null) {
            mDevOpenHelper.close();
            mDevOpenHelper = null;
        }

    }

    public void closeDaoSession() {
        if (mDaoSession != null) {
            mDaoSession.clear();
            mDaoSession = null;
        }

    }


}

